package com.java.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: wjr
 * @Description:
 * @Date Created in  2020-09-02 2:34
 * @Modified By:
 */
@WebServlet(value = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uName = req.getParameter("uName");
        if ("admin".equals(uName)){
            req.getSession().setAttribute("user",uName);
            resp.sendRedirect("/success.jsp");
        }else{
            Cookie u1 = new Cookie("u1", uName);
            resp.addCookie(u1);
            resp.sendRedirect("/index.jsp");
        }
    }
}
